Completed
Push — master ( 4ff74f...939158 )
by Askupa
02:11
created

Amarkal.settings.search.find   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 10
rs 9.4285
1
/**
2
 * The search class is used internally to search through the fields and find those
3
 * that match the user's query.
4
 */
5
Amarkal.settings.search = {
0 ignored issues
show
Bug introduced by
The variable Amarkal seems to be never declared. If this is a global, consider adding a /** global: Amarkal */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
6
    $fields: null,
7
    $input: null,
8
    init: function() {
9
        var _this = this;
10
        this.$fields = $('.amarkal-settings-field');
11
        this.$input = $('#settings-search');
12
        this.$input.on('keyup',function(){_this.onKeyup();});
13
    },
14
    onKeyup: function(e) {
0 ignored issues
show
Unused Code introduced by
The parameter e is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
15
        var query = this.$input.val().toLowerCase(),
16
            $matches;
17
    
18
        // The user's search query is longer than 1 character
19
        if(query.length > 1) {
20
            
21
            $matches = this.find(query);
22
            
23
            this.$fields.hide();
24
            $matches.show();
25
26
            // Textual user feedback
27
            $('#settings-search-results').text($matches.length ? $matches.length+' settings found' : 'Nothing found');
28
        }
29
30
        // The user's search query is not longer than 1 character
31
        else {
32
            this.$fields.show();
33
            $('#settings-search-results').text('');
34
        }
35
    },
36
    find: function(query) {
37
        var matches  = [];
38
        
39
        this.$fields.each(function(){
40
            var $field = $(this),
41
                title  = $field.attr('data-title').toLowerCase(),
42
                description = $field.find('.description').text();
43
44
            // Check query against the field's title
45
            if(title.match(query) || description.match(query)){
46
                matches.push($field);
47
            }
48
        });
49
50
        // Convert the matches array to a jQuery
51
        return $(matches).map(function(){return this.toArray();});
52
    }
53
};